import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex, requestFunHtml } from '@/utils';
import { useRequest } from 'umi';
import { getBaseUrl } from '@/services/urlconst';
import { ILoadMapTypeAll } from '@/common';
let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang';

/** ===================================================== 领用Serivice START ========================================== */
/** 暂存领用 */
export const addAssetApplyUesSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        state: `${getTenantID()}${ILoadMapTypeAll?.STORAGE_USE_STATUS}1000`,
        businessType: `${getTenantID()}${ILoadMapTypeAll?.ALLFLOW_TYPE}1011`,
      };

      params?.applyNumberAssetList?.forEach((item) => {
        item.useCompanyName = params?.useCompanyName;
        item.useCompanyID = params?.useCompanyID;
        item.useDepartmentName = params?.useDepartmentName;
        item.useDepartmentID = params?.useDepartmentID;
        item.usePositionName = params?.usePositionName;
        item.usePositionID = params?.usePositionID;
        item.userName = params?.userName;
        item.userID = params?.userID;
      });

      let reqUrl = `${baseUrl}/assetApplyUes/add`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    addAssetApplyUesRun: request?.run,
    addAssetApplyUesLoading: request?.loading,
  };
};

/** 获取领用详情 */
export const detailAssetApplyUesServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/assetApplyUes/detail/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    detailAssetApplyUesRun: request?.run,
    detailAssetApplyUesLoading: request?.loading,
  };
};

/** 编辑领用 */
export const editAssetApplyUesSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      params?.applyNumberAssetList?.forEach((item) => {
        item.useCompanyName = params?.useCompanyName;
        item.useCompanyID = params?.useCompanyID;
        item.useDepartmentName = params?.useDepartmentName;
        item.useDepartmentID = params?.useDepartmentID;
        item.usePositionName = params?.usePositionName;
        item.usePositionID = params?.usePositionID;
        item.userName = params?.userName;
        item.userID = params?.userID;
      });

      let reqUrl = `${baseUrl}/assetApplyUes/edit`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    editAssetApplyUesRun: request?.run,
    editAssetApplyUesLoading: request?.loading,
  };
};

/** 领用审批通过后调用 */
export const approvedAssetApplyUseSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        // createrID: getUserID(),
        // createPositionID: getPosIDs(),
        // createDepartmentID: getUserDepID(),
        // createCompanyID: getCompanyID(),
      };

      params?.applyNumberAssetList?.forEach((item) => {
        item.useCompanyName = params?.useCompanyName;
        item.useCompanyID = params?.useCompanyID;
        item.useDepartmentName = params?.useDepartmentName;
        item.useDepartmentID = params?.useDepartmentID;
        item.usePositionName = params?.usePositionName;
        item.usePositionID = params?.usePositionID;
        item.userName = params?.userName;
        item.userID = params?.userID;
      });

      let reqUrl = `${baseUrl}/assetApplyUes/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedAssetApplyUseRun: request?.run,
    approvedAssetApplyUseLoading: request?.loading,
  };
};

/** 校验锁定回退资产和物料数量 */
export const dealNumAssetApplyUseSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        // createrID: getUserID(),
        // createPositionID: getPosIDs(),
        // createDepartmentID: getUserDepID(),
        // createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyUes/dealNum/${payload.dealType}`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    dealNumAssetApplyUseRun: request?.run,
    dealNumAssetApplyUseLoading: request?.loading,
  };
};

/** ===================================================== 领用Serivice END ========================================== */

/** ===================================================== 退库Serivice START========================================== */
/** 暂存退库 */
export const addAssetApplyReturnSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        state: `${getTenantID()}${ILoadMapTypeAll?.STORAGE_RETURN_STATUS}1000`,
        businessType: `${getTenantID()}${ILoadMapTypeAll?.ALLFLOW_TYPE}1013`,
      };

      let reqUrl = `${baseUrl}/assetApplyReturn/add`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    addAssetApplyReturnRun: request?.run,
    addAssetApplyReturnLoading: request?.loading,
  };
};

/** 获取退库详情 */
export const detailAssetApplyReturnServices = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/assetApplyReturn/detail/${bodyParams?.id}/${getUserID()}`;
      return requestFunComponFiex('GET', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    detailAssetApplyReturnRun: request?.run,
    detailAssetApplyReturnLoading: request?.loading,
  };
};

/** 编辑领用 */
export const editAssetApplyReturnSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyReturn/edit`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    editAssetApplyReturnRun: request?.run,
    editAssetApplyReturnLoading: request?.loading,
  };
};

/** 退库审批通过后调用 */
export const approvedAssetApplyReturnSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        // createrID: getUserID(),
        // createPositionID: getPosIDs(),
        // createDepartmentID: getUserDepID(),
        // createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyReturn/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedAssetApplyReturnRun: request?.run,
    approvedAssetApplyReturnLoading: request?.loading,
  };
};

/**
 * 删除申请
 * @param callback 接口回调
 * @returns
 */
export const deleteApplyManageService = (callback: (res: any, clickType: boolean) => void) => {
  const deleteApplyManage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}${params?.url}`;
      let bodyParams = {
        ...params,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      delete bodyParams?.url;
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteApplyManageRun: deleteApplyManage?.run,
    deleteApplyManageLoading: deleteApplyManage?.loading,
  };
};
/** ===================================================== 退库Serivice END========================================== */

/**
 * 同步PDF
 * @param callback 接口回调
 * @returns
 */
export const syncPDFservices = (cb) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };

      let LY = '/assetApplyUes/syncWorkFlow';

      let TK = '/assetApplyReturn/syncWorkFlow';

      let reqUrl = '';

      if (reqParams?.queryType == '1') {
        // 领用
        reqUrl = baseUrl + LY;
      } else if (reqParams?.queryType == '2') {
        // 退库
        reqUrl = baseUrl + TK;
      } else {
        return;
      }

      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};

/**导出物资清单接口*/
export const ExportAssetsServices = () => {
  const request = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/assetApplyUes/assetApplyNumberExport/${payload?.id}/${payload?.key}`;
      return requestFunHtml('GET', reqUrl);
    },
    {
      manual: true,
    },
  );
  return {
    ExportAssetsRun: request.run,
    ExportAssetsLoading: request.loading,
  };
};

/**
 * 领用退库 - 管理页面 - 导出
 * */
export const manageExportEnterService = () => {
  const request = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/assetApplyUes/export`;
      let reqBody = {
        ...payload,
      };
      return requestFunHtml('POST', reqUrl, reqBody);
    },
    {
      manual: true,
    },
  );
  return {
    ExportEnterRun: request.run,
    ExportEnterLoading: request.loading,
  };
};
